var __base = {
    "init": function (modelArray) {
        var G = this;
        __base.common.call();
        //try {
        require(["allTpl", "mock"], function (template, Mock) {
            __base[modelArray[0]].call(G, template, Mock);
        });

        //} catch(error) {
        //console.error("body上找不到页面调用标示模块:", modelArray[0]);
        //console.error(error);
        //}

    },
    "common": function () {
        var host = _CONFIG[__webState]["AJAX_URL"];
        // 接口地址
        __base.url = {
            //商品详情
            detailed: {
                //商品详情
                "OrderDetail": host + "api/BookOrder/OrderDetail",
                //订单取消 未支付状态下的取消
                "Cancel": host + "api/BookOrder/Cancel",
                //确认收货
                "ConfirmRece": host + "api/BookOrder/ConfirmRece",
                //去支付
                "Pay": host + "api/BookOrder/Pay"
            },
            //已经支付的情况 取消订单或退换货
            //          OrderSysNo: 56,
            //          Content: "内容",
            //          Title: "标题",
            //          OperType: 0,//0取消1退货3换货
            //          MobilePhone:"18221292401",
            //          ValidateCode:""
            "OrderOper": host + "api/BookOrder/OrderOper",
            //订单统计
            "GetPersonOrderStatus": host + "api/BookOrder/GetPersonOrderStatus"
        };

        // 请求处理函数
        __base.AjaxFun = function (para, callback, error) {
            $.ajax({
                url: para["url"],
                type: para["type"] || "post",
                //type: "get",
                dataType: "json",
                data: para["data"] || {},
                success: function (data) {
                    callback(data);
                }
            }).fail(
                function (xhr, textStatus, err) {
                    if (typeof (error) === "function") {
                        error(xhr, textStatus, err);
                    } else {
                        console.log(xhr, textStatus, err);
                        popWrap.alert({content:err})
                    }
            }).always(function(){
                ajaxLoading.remove();
            });
        };

    },
    //个人中心首页
    "PersonalCenterHome":function(){
        require(["views/PersonalCenter/home.min"], function (app) {
            app.init()
        });
    },
    //个人资料
    "personalData":function(template){
        require(["views/PersonalCenter/personalData.min","page","validate"], function (app) {
            app.init()
        });
    },
    //订单列表
    "orderList" : function(template){
        require(["views/PersonalCenter/orderList.min"], function (app) {
            app.init()
        });
    } ,
    //订单详情
    "detailed" : function(template){
        //0为成功 1为失败 2跳登录 3跳订单列表

        //__base.url.detailed.OrderDetail = "http://10.0.10.59:9000/OrderDetail";
        //__base.url.OrderOper = "http://10.0.10.59:9000/OrderOper";
        //__base.url.detailed.ConfirmRece = "http://10.0.10.59:9000/ConfirmRece";

        var __orderNo = parse_url(window.location.href)["orderNo"];

        __base.AjaxFun({
            url: __base.url.detailed.OrderDetail,
            data:{
                orderNo: __orderNo
            }
        },function(data){
            console.log(data);
            if (data["Code"] === "0") {
                var json = data["Data"],
                    Receiving = json['Receiving'][0],
                $J_detailedIntro = $("#J_detailedIntro"),
                $J_pay = $("#J_pay"),
                $J_toolsBox = $("#J_toolsBox"),
                $J_number = $("#J_number"),
                $J_invoice = $("#J_invoice"),
                $J_orderInfo_detailed = $("#j_orderInfo_detailed"),
                nowPoint = 0,
                nowPrice = 0,
                //已经支付显示订单支付信息
                showOrderInfo_detailed = function(){
                    $J_number.hide();
                    $J_orderInfo_detailed.html([
                        '	<dl>',
                        '		<dt>订单编号</dt>',
                        '		<dd>'+json['SysNo']+'</dd>',
                        '	</dl>',
                        '	<dl>',
                        '		<dt>订单金额</dt>',
                        '		<dd class="priceBox">',
                        '			<span class="point">积分<i> ' + json['Points'] + '</i> + </span>',
                        '			<span class="price">现金￥<i>' + json["Amount"] + '</i></span>',
                        '		</dd>',
                        '	</dl>',
                        '	<dl>',
                        '		<dt>支付时间</dt>',
                        '		<dd>2015-11-07 17:07:09</dd>',
                        '	</dl>'].join("")).show();
                };

                $J_orderInfo_detailed.hide();

                //订单编号
                $J_number.find(".con").text(json['SysNo']);

                //买家信息
                $J_detailedIntro.find(".name").text(Receiving['ReceivingName']);
                $J_detailedIntro.find(".tright").text(Receiving['MobilePhone']);
                $J_detailedIntro.find(".detailedAddes").text(Receiving['ReceivingAddress']);

                //支付金额
                $J_pay.find(".payType").text(json['PayType']);
                $J_pay.find(".conDetailed .price").text("￥"+json['Amount']);
                $J_pay.find(".conDetailed .point").text(json['Points']);
                $J_pay.find(".y_price").text('￥0.00');
                $J_pay.find(".money>.point").text(json['Points']);
                $J_pay.find(".money>.price").text(json['Amount']);

                //商品列表
               var html_detailedList="";
                $.each(json['Product'], function(i ,item){
                    //html_detailedList += ['<a href="'+  _CONFIG[__webState]["goodsPage_URL"]+"?id=" +  item["ProductSysNo"] + '"  class="listData picList">',
                    //    '	<div class="picl"><img src="'+ item["Pic"] +'"></div>',
                    //    '	<div class="middle">',
                    //    '		<div class="con">'+ item["ProductName"] +'</div>',
                    //    '		<div class="moneym">积分<b class="point">'+ item["Points"] +'</b> +现金<b class="price">'+ item["Amount"] +'</b></div>',
                    //    '	</div>',
                    //    '	<div class="right"></div>',
                    //    '</a>'].join("");
                    html_detailedList += ['<a href="'+  _CONFIG[__webState]["goodsPage_URL"]+"?id=" +  item["ProductSysNo"] + '">',
                        '	<img src="'+ item["Pic"] +'" class="pic">',
                        '	<div class="con">',
                        '		<h4>'+ item["ProductName"] +'</h4>',
                        '		<div class="bBox">',
                        '			<span class="point">积分<i>'+ item["Points"] +'</i></span>',
                        '			<span class="price">现金￥<i>'+ item["Amount"] +'</i></span>',
                        '			<span class="count">x'+ item["Quantity"] +'</span>',
                        '		</div>',
                        '	</div>',
                        '	<div class="icon-next">&#xe91f</div>',
                        '</a>'].join("");
                });
                $("#J_detailedList").html(html_detailedList);


                //发票
                var html_invoice = "";
                if (json["Invoice"]){
                    $.each(json["Invoice"], function(i, item){
                        html_invoice += ['<dl class="hd">',
                            '	<dt>发票信息：</dt>',
                            '	<dd></dd>',
                            '</dl>',
                            '<div class="conDetailed">',
                            '	<dl><dt>发票抬头：</dt><dd class="til">'+ (function(){
                                return item['InvoiceTitle'] || "无";
                            })() + '</dd></dl>',
                            '	<dl><dt>发票内容：</dt><dd class="con">'+ (function(){
                                return item['InvoiceContent'] || "无";
                            })() + '</dd></dl>',
                            '</div>'].join("");
                    });
                    $J_invoice.html(html_invoice).show();
                }

                $J_toolsBox.hide();
                //0 等待支付  去支付按钮 和取消按钮
                //3 支付成功  取消按钮
                //19 已发货   确认收货按钮
                //20 已完成   退换货按钮
                switch (json["Status"]){
                    case 0:
                        $J_number.show();
                        $J_toolsBox.html(['<div class="ld">',
                            '	<p class="point">积分<i>' + json['Points']+ '</i></p>',
                            '	<p class="price">现金￥<i>' + json["Amount"] + '</i></p>',
                            '</div>',
                            '<div class="rd">',
                            '	<span id="J_toolsBuy">去支付</span>',
                            '</div>',
                            '<div class="md">',
                            '	<span id="J_closeNotPay">取消订单</span>',
                            '</div>'].join("")).show();
                        break;
                    case 3:
                        showOrderInfo_detailed();
                        $J_toolsBox.html(['<div class="priceBox">',
                            '	<dl>',
                            '		<dt>积分</dt>',
                            '		<dd> ' + json['Points'] + '</dd>',
                            '	</dl>',
                            '	<dl>',
                            '		<dt>现金</dt>',
                            '		<dd><i>￥</i>' + json["Amount"] + '</dd>',
                            '	</dl>',
                            '	<div class="rd_btn">',
                            '		<span id="J_toolsClose">取消订单</span>',
                            '	</div>',
                            '</div>'].join("")).show();
                        break;
                    case 19:
                        showOrderInfo_detailed();
                        $J_toolsBox.html(['<div class="priceBox">',
                            '	<dl>',
                            '		<dt>积分</dt>',
                            '		<dd> ' + json['Points'] + '</dd>',
                            '	</dl>',
                            '	<dl>',
                            '		<dt>现金</dt>',
                            '		<dd><i>￥</i>' + json["Amount"] + '</dd>',
                            '	</dl>',
                            '	<div class="rd_btn">',
                            '		<span id="J_toolsComplete">确认收货</span>',
                            '	</div>',
                            '</div>'].join("")).show();
                        break;
                    case 20:
                        showOrderInfo_detailed();
                        $J_toolsBox.html(['<div class="priceBox">',
                            '	<dl>',
                            '		<dt>积分</dt>',
                            '		<dd> ' + json['Points'] + '</dd>',
                            '	</dl>',
                            '	<dl>',
                            '		<dt>现金</dt>',
                            '		<dd><i>￥</i>' + json["Amount"] + '</dd>',
                            '	</dl>',
                            '	<div class="rd_btn">',
                            '		<span id="J_toolsExchange">申请退换货</span>',
                            '	</div>',
                            '</div>'].join("")).show();
                        break;
                }

                //订单未支付状态下 - 取消订单
                $("#J_closeNotPay").on('click', function(){
                    popWrap.defult({title:"警告",
                        content: '请确认是否取消该订单',
                        confirm:function(){
                            popWrap.alert({content:"正在取消",autoClose:false});
                            __base.AjaxFun({
                                url: __base.url.detailed.Cancel,
                                data:{
                                    orderNo: __orderNo
                                }
                            },function(data2){
                                popWrap.alertRemove();
                                if (data2['Code'] === "0"){
                                    popWrap.alert({content:"取消成功"});
                                    setTimeout(function(){
                                        window.location.reload();
                                    },2000);
                                } else {
                                    popWrap.alert({content:data2['Message']});
                                }
                            },function(xhr, textStatus, err){
                                popWrap.alert({content:"取消失败"});
                                console.log(xhr, textStatus, err);
                            })
                        }});
                });

                //订单已经支付的情况 - 取消订单或退换货
                $("#J_toolsClose").on('click', function(){
                    popWrap.defult({title:"取消订单",
                        content: '<textarea name="" id="J_textarea" cols="30" rows="5" placeholder="请输入取消订单理由"></textarea>',
                        confirm:function(){

                            var content = $.trim( $("#J_textarea").val() );
                            if ( !content ){
                                popWrap.alert({content:"请输入取消订单理由", autoClose:1000});
                                setTimeout(function(){
                                    $("#J_toolsClose").trigger("click");
                                },1300);
                                return false;
                            }

                        popWrap.alert({content:"正在处理",autoClose:false});
                        __base.AjaxFun({
                            url: __base.url.OrderOper,
                            data:{
                                OrderSysNo: __orderNo,
                                Content: content ,
                                Title:"取消订单",
                                OperType: 0, //0取消1退换货
                                MobilePhone: Receiving['MobilePhone'],
                                ValidateCode:""
                            }
                        },function(data_toolsClose){
                            popWrap.alertRemove();
                            if (data_toolsClose['Code'] === "0"){
                                popWrap.alert({content:"处理成功"});
                                setTimeout(function(){
                                    window.location.reload();
                                },2000);
                            } else {
                                popWrap.alert({content:data_toolsClose['Message']});
                            }
                        },function(xhr, textStatus, err){
                            popWrap.alert({content:"取消失败"});
                            console.log(xhr, textStatus, err);
                        })
                    }});
                });

                //去支付
                $("#J_toolsBuy").on('click', function(){
                    popWrap.alert({content:"正在处理",autoClose:false});
                    __base.AjaxFun({
                        url: __base.url.detailed.Pay,
                        data:{
                            orderNo: __orderNo
                        }
                    },function(data2){
                        popWrap.alertRemove();

                        switch (data2['Code']){
                            case "0":
                                popWrap.alert({content:"正在跳转"});
                                console.log(data2["Data"]);
                                window.location.href = data2["Data"];
                                break;
                            case "1":
                                popWrap.alert({content:data2["Message"]});
                                break;
                            case "2":
                                popWrap.alert({content:"跳转登录"});
                                break;
                        }

                        //0为成功 1为失败 2跳登录 3跳订单列表

                    },function(xhr, textStatus, err){
                        popWrap.alert({content:"处理失败"});
                        console.log(xhr, textStatus, err);
                    })
                });

                //确认收货
                $("#J_toolsComplete").on('click', function(){
                    popWrap.defult({title:"警告",content:"是否确认收货",confirm:function(){
                        popWrap.alert({content:"正在确认收货",autoClose:false});
                        __base.AjaxFun({
                            url: __base.url.detailed.ConfirmRece,
                            data:{
                                orderNo: __orderNo
                            }
                        },function(data2){
                            popWrap.alertRemove();
                            if (data2['Data'] === "0"){
                                popWrap.alert({content:"确认收货成功"});
                            } else {
                                popWrap.alert({content:data2['Message']});
                            }
                        },function(xhr, textStatus, err){
                            popWrap.alert({content:"确认收货失败"});
                            console.log(xhr, textStatus, err);
                        })
                    }});
                });

                //申请退换货
                $("#J_toolsExchange").on('click', function(){
                     window.location.href = _CONFIG[__webState].orderAfterSales + "?orderNo=" + __orderNo ;
                });

            } else {
                popWrap.alert({content:data["Message"]});
            }

        })

    },
    //申请售后服务
    "aftersales": function(){

        //__base.url.OrderOper = "http://10.0.10.59:9000/OrderOper";

        require(["validate"],function(){
            var __orderNo = parse_url(window.location.href)["orderNo"];

            __base.AjaxFun({
                url: __base.url.detailed.OrderDetail,
                data:{
                    orderNo: __orderNo
                }
            },function(data){
                console.log(data);
                if (data["Code"] === "0") {
                    var json = data["Data"],
                        $J_name = $("#J_name"),
                        $J_phone = $("#J_phone"),
                        $J_returns = $("#J_returns"),
                        $J_exchange = $("#J_exchange"),
                        $J_textAreaCon = $("#J_textAreaCon"),
                        $J_detailedList = $("#J_detailedList"),
                        $J_submit = $("#J_submit"),
                        serverType = -1, //0取消1退货3换货
                        serverTitle = "",
                        getTextAreaCon = function(){
                            return $.trim($J_textAreaCon.val());
                        },
                        getPhone = function(){
                            return $.trim($J_phone.val());
                        };

                    //商品列表
                    var html_detailedList="";
                    $.each(json['Product'], function(i ,item){
                        html_detailedList += ['<a href="'+  _CONFIG[__webState]["goodsPage_URL"]+"?id=" +  item["ProductSysNo"] + '">',
                            '	<img src="'+ item["Pic"] +'" class="pic">',
                            '	<div class="con">',
                            '		<h4>'+ item["ProductName"] +'</h4>',
                            '		<div class="bBox">',
                            '			<span class="point">积分<i>'+ item["Points"] +'</i></span>',
                            '			<span class="price">现金￥<i>'+ item["Amount"] +'</i></span>',
                            '			<span class="count">x'+ item["Quantity"] +'</span>',
                            '		</div>',
                            '	</div>',
                            '	<div class="icon-next">&#xe91f</div>',
                            '</a>'].join("");
                    });
                    $J_detailedList.html(html_detailedList);


                    //服务类型
                    $J_returns.on('click', function(){
                        $(this).addClass("current").siblings().removeClass("current");
                        serverType = 1;
                        serverTitle = "退货";
                    });
                    $J_exchange.on('click', function(){
                        $(this).addClass("current").siblings().removeClass("current");
                        serverType = 3;
                        serverTitle = "换货";
                    });

                    $J_submit.on('click', function(){
                        if ( !$J_returns.hasClass("current") && !$J_exchange.hasClass("current") ){
                            popWrap.alert({content: "请选择服务类型！",autoClose:800});
                            return false;
                        }
                        if ( !getTextAreaCon() ){
                            popWrap.alert({content: "请填写问题描述！",autoClose:800});
                            return false;
                        }
                        if ( !validate['isName2']( $.trim($J_name.val()) ) ){
                            popWrap.alert({content: "请填写正确的联系人！",autoClose:800});
                            return false;
                        }
                        if ( !validate['isMobile']( getPhone() ) ){
                            popWrap.alert({content: "请填写正确的联系电话",autoClose:800});
                            return false;
                        }

                        popWrap.alert({content:"正在处理",autoClose:false});
                        __base.AjaxFun({
                            url: __base.url.OrderOper,
                            data:{
                                OrderSysNo: __orderNo,
                                Content: getTextAreaCon(),
                                Title: serverTitle,
                                OperType: serverType, //0取消1退货3换货
                                MobilePhone: getPhone(),
                                ValidateCode:""
                            }
                        },function(data_toolsClose){
                            popWrap.alertRemove();
                            if (data_toolsClose['Code'] === "0"){
                                popWrap.alert({content:"处理成功"});
                                setTimeout(function(){
                                    window.location.href = _CONFIG[__webState].orderDetails + "?orderNo=" + __orderNo ;
                                },2000);
                            } else {
                                popWrap.alert({content:data_toolsClose['Message']});
                            }
                        },function(xhr, textStatus, err){
                            popWrap.alert({content:"处理失败"});
                            console.log(xhr, textStatus, err);
                        })
                    });

                } else {
                    popWrap.alert({content:data["Message"]});
                }

            })
        });


    },
    //申请售后服务 - 订单列表
    "aftersaleslist":function(){
        ajaxLoading.remove();
    }


};
